/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package imageProcessing.classes;

import java.awt.image.BufferedImage;
import ProcesamientoImagenes.interfaces.MaskApplicator;

/**
 *
 * @author damfercer
 */
public class AlphaMaskApplicator implements MaskApplicator{
    private BufferedImage mask;
    private BufferedImage image;
    
    public AlphaMaskApplicator(BufferedImage image, BufferedImage mask){
        this.image=ImageHelper.getType4ByteABGRBufferedImage(image);
        this.mask=ImageHelper.getType4ByteABGRBufferedImage(mask);
    }
    @Override
    public BufferedImage applyMaskToImage() {
        return this.applyMaskToImage(this.image, this.mask);
    }

    @Override
    public BufferedImage applyMaskToImage(BufferedImage image, BufferedImage mask) {
        BufferedImage resImage= ImageHelper.deepCopy(image);
        final int width = resImage.getWidth();
        int[] imgData = new int[width];
        int[] maskData = new int[width];

        for (int y = 0; y < resImage.getHeight(); y++) {
            // fetch a line of data from each image
            resImage.getRGB(0, y, width, 1, imgData, 0, 1);
            mask.getRGB(0, y, width, 1, maskData, 0, 1);
            // apply the mask
            for (int x = 0; x < width; x++) {
                int color = imgData[x] & 0x00FFFFFF; // mask away any alpha present
                int maskColor = (maskData[x] & 0x00FF0000) << 8; // shift red into alpha bits
                color |= maskColor;
                imgData[x] = color;
            }
    // replace the data
            resImage.setRGB(0, y, width, 1, imgData, 0, 1);
        }
        return resImage;
    }
    
}
